//迭代器设计模式
const todos = {
  life: ["吃饭", "睡觉", "做梦"],
  learn: ["语文", "数学", "英语"],
  work: ["喝茶"],
  each: function (callback) {
    const all = [].concat(this.life, this.learn, this.work);
    for (const item of all) {
      callback(item);
    }
  },
  [Symbol.iterator]: function () {
    const all = [...this.life, ...this.learn, ...this.work];
    let index = 0;
    return {
      next: function () {
        return {
          value: all[index],
          done: index++ >= all.length,
        };
      },
    };
  },
};

todos.each((item) => {
  console.log(item);
});
//吃饭 睡觉 做梦 语文 数学 英语 喝茶
console.log("-----------------------------------------------");
for (const item of todos) {
  console.log(item);
}
//吃饭 睡觉 做梦 语文 数学 英语 喝茶
